package Secondpagh;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.gyf.immersionbar.ImmersionBar;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import SHU.water2.R;
import SHU.water2.Spinnerstyle;
import ThirdPagh.rainLevelList;

public class rainLevelChart extends AppCompatActivity implements View.OnClickListener {
    private LineChart lc;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rainlevelchart);
        ImmersionBar.with(this).statusBarColor(R.color.color).fitsSystemWindows(true).init();

        click();

        lc = findViewById(R.id.lc);//获取控件
        //lc.notifyDataSetChanged();//刷新数据
        //lc.invalidate();//重绘

        setDesc();
        setXAxis();
        setYAxis();
        loadData();

        MyMarkerView mv = new MyMarkerView(this);
        lc.setMarker(mv);

        spinerset();

    }

    private void click() {
        findViewById(R.id.ret1).setOnClickListener(this);
        findViewById(R.id.check1).setOnClickListener(this);
        findViewById(R.id.check2).setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.check1:

                break;

            case R.id.check2:
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), rainLevelList.class);
                startActivity(intent);
                break;

            case R.id.ret1:
                this.finish();
                break;
        }
    }


    // * 设置为强制横屏
    @Override
    protected void onResume() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        super.onResume();
    }

    public void setDesc() {
        Description description = new Description();
        description.setText("二官塘泵闸24小时雨量");//设置文本
        description.setTextSize(18f); //设置文本大小
        description.setTextColor(Color.BLACK);//设置文本颜色

        //获取屏幕的中间位置
        WindowManager wm = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        float x = (dm.widthPixels / 2) + 180;

        description.setPosition(x, 50);//设置文本的位置
        lc.setDescription(description);//添加给LineChart

        //图例
        Legend legend = lc.getLegend();
        legend.setDrawInside(false);//不绘制在图形内部
        legend.setTextColor(Color.BLACK);//设置文字颜色
        legend.setFormSize(10f);//设置图例的大小
        legend.setTextSize(10f);
    }

    private void setYAxis() {
        //得到YAxis对象
        YAxis yAxis1 = lc.getAxisLeft();
        lc.getAxisRight().setEnabled(false);//不显示右侧的y轴

        //设置字体颜色白色
        yAxis1.setTextColor(Color.parseColor("#2D93F6"));
        //设置最大值和最小值
        yAxis1.setAxisMaximum(6);
        yAxis1.setAxisMinimum(-1);
        //设置字体大小
        yAxis1.setTextSize(10f);
        //字体加粗
        yAxis1.setTypeface(Typeface.DEFAULT_BOLD);
        //yAxis1.setGridColor(Color.WHITE);//网格颜色
        //强制只绘制5个Label
        yAxis1.setLabelCount(8, true);
        yAxis1.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                String tep = value + "";
                return tep.substring(0, tep.indexOf(".")) + "mm";
            }
        });


        //限制线
        LimitLine outlimit = new LimitLine(3.1f, "");
        outlimit.enableDashedLine(10f, 5f, 5f);//虚线
        //line.setLineColor(Color.parseColor("#FA8874"));
        outlimit.setLineColor(Color.parseColor("#2D93F6"));
        outlimit.setLineWidth(2f);


        yAxis1.addLimitLine(outlimit);
    }

    public void setXAxis() {
        //得到XAxis对象
        XAxis xAxis = lc.getXAxis();
        xAxis.setTextColor(Color.BLACK);//字体颜色
        //设置最大最小值
        xAxis.setAxisMaximum(23);
        xAxis.setAxisMinimum(0);
        xAxis.setGranularity(0);//设置间隔
        //将x显示在下方
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(10f);//字体大小
        xAxis.setLabelCount(24);
        xAxis.setDrawGridLines(false);
    }

    private void loadData() {
        //设置边距
        lc.setExtraOffsets(30, 60, 30, 10);
        //不绘制图例和标题
        //lc.getLegend().setEnabled(false);
        //lc.getDescription().setEnabled(false);

        float outriver[] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2};
        List<Entry> yData1 = new ArrayList<Entry>();

        for (int i = 0; i < outriver.length; i++) {
            yData1.add(new Entry(i, outriver[i]));
        }


        LineDataSet lineDataSet1 = new LineDataSet(yData1, "雨量");

        lineDataSet1.setLineWidth(3);//设置线条宽度
        lineDataSet1.setColor(Color.parseColor("#2D93F6"));//设置线条颜色
        lineDataSet1.setDrawCircles(false);
        lineDataSet1.setDrawValues(false);//不绘制文本
        lineDataSet1.setMode(LineDataSet.Mode.CUBIC_BEZIER);//设置为贝塞尔曲线
        lineDataSet1.setCubicIntensity(0.05f);//曲线强度0.1
        lineDataSet1.setHighLightColor(Color.RED);//设置点击交点后显示交高亮线的颜色
        lineDataSet1.setHighlightLineWidth(1f);


        LineData lineData = new LineData(lineDataSet1);

        lc.setData(lineData);
        lc.setScaleEnabled(false);// 是否可以缩放
        lc.setDoubleTapToZoomEnabled(false);//设置是否可以通过双击屏幕放大图表。
    }

    //自定义标记
    public class MyMarkerView extends MarkerView {

        private TextView tvContent;
        private DecimalFormat format = new DecimalFormat("##0");

        public MyMarkerView(Context context) {
            super(context, R.layout.markview);
            tvContent = (TextView) findViewById(R.id.MarkContent);
        }

        @Override
        public void refreshContent(Entry e, Highlight highlight) {

            tvContent.setText(format.format(e.getX()) + ":00" + "   " + format.format(e.getY()) + "mm");

            super.refreshContent(e, highlight);
        }

        @Override
        public MPPointF getOffset() {
            return new MPPointF(-(getWidth() / 2), -getHeight());
        }
    }


    public void spinerset() {
        Spinner spinner1;
        Spinner spinner2;
        Spinner spinner3;
        Spinner spinner4;
        Spinner spinner5;
        Spinner spinner6;

        ArrayAdapter<String> arr_adapter1;
        ArrayAdapter<String> arr_adapter2;
        ArrayAdapter<String> arr_adapter3;
        ArrayAdapter<String> arr_adapter4;
        ArrayAdapter<String> arr_adapter5;
        ArrayAdapter<String> arr_adapter6;

        spinner1 = findViewById(R.id.spinner1);
        spinner2 = findViewById(R.id.spinner2);
        spinner3 = findViewById(R.id.spinner3);
        spinner4 = findViewById(R.id.spinner4);
        spinner5 = findViewById(R.id.spinner5);
        spinner6 = findViewById(R.id.spinner6);
        String[] items1 = getResources().getStringArray(R.array.year);
        String[] items2 = getResources().getStringArray(R.array.months);
        String[] items3 = getResources().getStringArray(R.array.days);
        String[] items4 = getResources().getStringArray(R.array.location_zhen);
        String[] items5 = getResources().getStringArray(R.array.location_xu);
        String[] items6 = getResources().getStringArray(R.array.site);


        //适配器1
        arr_adapter1 = new Spinnerstyle(this, items1);
        //设置样式
        arr_adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner1.setAdapter(arr_adapter1);

        //适配器2
        arr_adapter2 = new Spinnerstyle(this, items2);
        //设置样式
        arr_adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner2.setAdapter(arr_adapter2);

        //适配器3
        arr_adapter3 = new Spinnerstyle(this, items3);
        //设置样式
        arr_adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner3.setAdapter(arr_adapter3);

        //适配器3
        arr_adapter4 = new Spinnerstyle(this, items4);
        //设置样式
        arr_adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner4.setAdapter(arr_adapter4);

        //适配器3
        arr_adapter5 = new Spinnerstyle(this, items5);
        //设置样式
        arr_adapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner5.setAdapter(arr_adapter5);

        //适配器3
        arr_adapter6 = new Spinnerstyle(this, items6);
        //设置样式
        arr_adapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner6.setAdapter(arr_adapter6);
    }
}







